package com.seveneight.baijiesteward.ui.main.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.codbking.widget.DatePickDialog;
import com.codbking.widget.OnSureLisener;
import com.codbking.widget.bean.DateType;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.adapter.RepaymentAdapter;
import com.seveneight.baijiesteward.base.BaseActivity;
import com.seveneight.baijiesteward.base.MessageBean;
import com.seveneight.baijiesteward.bean.AnyBean;
import com.seveneight.baijiesteward.bean.BranchBean;
import com.seveneight.baijiesteward.bean.PlanDetailListBean;
import com.seveneight.baijiesteward.bean.RepayPlanBean;
import com.seveneight.baijiesteward.bean.UpdatePlanDetailListBean;
import com.seveneight.baijiesteward.http.MyCallBack;
import com.seveneight.baijiesteward.ui.user.model.RepayPlanCardModel;
import com.seveneight.baijiesteward.ui.user.model.UpdatePlanListModel;
import com.seveneight.baijiesteward.utils.DateUtil;
import com.seveneight.baijiesteward.utils.MyListView;
import com.seveneight.baijiesteward.utils.StringUtils;
import com.seveneight.baijiesteward.views.BranchBankListDialog;

import org.greenrobot.eventbus.Subscribe;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 预约还款
 */
public class RepaymentScheduleActivity extends BaseActivity implements View.OnClickListener{
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.pay_time)
    TextView pay_time;
    @BindView(R.id.card_money)
    EditText card_money;
    @BindView(R.id.repay_amount)
    EditText repay_amount;
    @BindView(R.id.formulation_pay_layout)
    LinearLayout formulation_pay_layout;
    @BindView(R.id.re_submit)
    LinearLayout re_submit;
    @BindView(R.id.latest_time)
    TextView latest_time;
    @BindView(R.id.repay_count)
    TextView repay_count;
    @BindView(R.id.money_number)
    TextView money_number;
    @BindView(R.id.recharge_count)
    TextView recharge_count;
    @BindView(R.id.bt_submit)
    TextView bt_submit;

    MyListView myListView;

    UpdatePlanListModel upadateApi=new UpdatePlanListModel();
    RepayPlanCardModel api=new RepayPlanCardModel();
    RepayPlanBean repayPlanBean;
    private BranchBankListDialog branchBankListDialog;
    BranchBean dateBean;
    private List<BranchBean> dateData=new ArrayList<>();
    RepaymentAdapter mAdapter;
    List<PlanDetailListBean> listDate=new ArrayList<>();
    List<PlanDetailListBean> relistDate=new ArrayList<>();
    String cardId,cardNo;


//    item_re_pay_list_layout.xml

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_repayment_schedule);
    }

    @Override
    protected void initView() {
        tv_title.setText("设置还款计划");
        formulation_pay_layout.setVisibility(View.GONE);
        bt_submit.setVisibility(View.GONE);
        myListView=findViewById(R.id.repay_list);
        cardId=getIntent().getStringExtra("cardId");
        cardNo=getIntent().getStringExtra("cardNo");
    }

    @Override
    protected void initData() {
        mAdapter=new RepaymentAdapter(this,relistDate);
        myListView.setAdapter(mAdapter);
        addDate();
    }
    public void addDate(){
        for (int i=1;i<29;i++){
            BranchBean bean=new BranchBean();
            bean.setCode(i+"");
            bean.setKey(i+"");
            bean.setText("每月"+i+"号");
            dateData.add(bean);
        }
    }

    @Override
    protected void setListener() {

    }

    @Subscribe
    public void onEvent(MessageBean messageBean){
        switch (messageBean.getType()){
            case 11:
//                dateBean1 = (BranchBean) messageBean.getData();
//                bill_date.setText("每月"+dateBean1.getKey()+"号");
                break;
            case 12:
//                dateBean2 = (BranchBean) messageBean.getData();
//                pay_date.setText("每月"+dateBean2.getKey()+"号");
                break;
        }
    }
    /**
     * 选择行业
     */
    public void showDialog(){
        branchBankListDialog = new BranchBankListDialog(activity, 11, dateData);
        branchBankListDialog.show();
    }

    Map<String,String> params;
    @OnClick({R.id.iv_back,R.id.pay_tiem_layout,R.id.pay_time,R.id.bt_next,R.id.bt_submit,R.id.re_submit})
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.pay_tiem_layout:
                selectDate();
//                branchBankListDialog = new BranchBankListDialog(activity, 11, dateData);
//                branchBankListDialog.show();
                break;
            case R.id.pay_time:
                selectDate();
//                branchBankListDialog = new BranchBankListDialog(activity, 11, dateData);
//                branchBankListDialog.show();
                break;
            case R.id.bt_next:
                requstDate();
                break;
            case R.id.re_submit:
                requstDate();
                break;
            case R.id.bt_submit://修改账单
                List<UpdatePlanDetailListBean> updateList=new ArrayList<>();
                for (int i=0;i<relistDate.size();i++){
                    Log.i("xxxxxxxldb",JSON.toJSONString(relistDate.get(i)));
                    updateList.add(0,JSON.parseObject(JSON.toJSONString(relistDate.get(i)),UpdatePlanDetailListBean.class));
                    List<PlanDetailListBean> arrayLsit=relistDate.get(i).getList();
                    if (null!=arrayLsit){
                        for (int y=0;y<arrayLsit.size();y++){
                            updateList.add(0,JSON.parseObject(JSON.toJSONString(arrayLsit.get(y)),UpdatePlanDetailListBean.class));
                        }
                    }

                }
                params=new HashMap<>();
                params.put("RechargeAmount",repayPlanBean.getRepayAmount());
                params.put("RechargeCount",repayPlanBean.getRechargeCount());
                params.put("RepayAmount",repayPlanBean.getRepayAmount());
                params.put("RepayCount",repayPlanBean.getRepayCount());
                params.put("PlanMode","0");

                JSONArray jsonArray = new JSONArray();
                JSONObject tmpObj = null;
                int count = updateList.size();
                for(int i = 0; i < count; i++)
                {
                    tmpObj = new JSONObject();
                    try {
                        tmpObj.put("PlanType" , updateList.get(i).getPlanType());
                        tmpObj.put("CreditCardId", cardId);
                        tmpObj.put("CreditCardNo", cardNo);
                        tmpObj.put("Amount", updateList.get(i).getAmount());
                        tmpObj.put("ExecuteTime", updateList.get(i).getExecuteTime());
                        tmpObj.put("ProductCategoryType", updateList.get(i).getProductCategoryType());
                        jsonArray.put(tmpObj);
                        tmpObj = null;
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                upadateApi.getBankList(this, jsonFormat.updateRepayData(params,jsonArray), new MyCallBack<AnyBean>() {
                        @Override
                        public void onSuccess(AnyBean anyBean) {
                            finish();
                            startActivity(new Intent(getContext(),PlanPayListActivity.class));
                        }

                    @Override
                    public void onFailed() {

                    }
                });

                break;
        }
    }
    public void requstDate(){
        if (StringUtils.isEmpty(repay_amount.getText().toString())){
            Toast.makeText(this, "请输入还款总金额", Toast.LENGTH_SHORT).show();
            return;
        }
        if (StringUtils.isEmpty(card_money.getText().toString())){
            Toast.makeText(this, "请输入信用卡余额", Toast.LENGTH_SHORT).show();
            return;
        }
        params=new HashMap<>();
        params.put("BillAmount",repay_amount.getText().toString());
        params.put("LastRepaymentDate",pay_time.getText().toString());
        params.put("BalanceInCard",card_money.getText().toString());
        api.getBankList(this, jsonFormat.AddCard(params), new MyCallBack<RepayPlanBean>() {
            @Override
            public void onSuccess(RepayPlanBean anyBean) {
                relistDate.clear();
                repayPlanBean=anyBean;
                formulation_pay_layout.setVisibility(View.VISIBLE);
                bt_submit.setVisibility(View.VISIBLE);
                repay_count.setText("共"+anyBean.getRepayCount()+"笔");
                money_number.setText("还款金额:"+anyBean.getRepayAmount()+"元");
                recharge_count.setText("手续费:"+anyBean.getTotalFee());
                listDate= JSON.parseArray(anyBean.getPlanDetailList(),PlanDetailListBean.class);
                List<PlanDetailListBean> data=new ArrayList<>();
                for (int i=0;i<listDate.size();i++){
                    if (listDate.get(i).getPlanType().equals("1")){
                        PlanDetailListBean bean=new PlanDetailListBean();
                        bean.setList(data);
                        bean.setActualAmount(listDate.get(i).getActualAmount());
                        bean.setAmount(listDate.get(i).getAmount());
                        bean.setExecuteTime(listDate.get(i).getExecuteTime());
                        bean.setFee(listDate.get(i).getFee());
                        bean.setPlanType(listDate.get(i).getPlanType());
                        relistDate.add(bean);
                        data=new ArrayList<>();
                    }else {
                        data.add(listDate.get(i));
                    }
                }
                mAdapter.refresh(relistDate);
            }

            @Override
            public void onFailed() {

            }
        });
    }
    public void selectDate(){
        DatePickDialog dialog = new DatePickDialog(this);
        //设置上下年分限制
        dialog.setYearLimt(0);
        //设置标题
        dialog.setTitle("选择时间");
        //设置类型
        dialog.setType(DateType.TYPE_YMD);
        //设置消息体的显示格式，日期格式
        dialog.setMessageFormat("yyyy-MM-dd");
        //设置选择回调
        dialog.setOnChangeLisener(null);
        //设置点击确定按钮回调
        dialog.setOnSureLisener(new OnSureLisener() {
            @Override
            public void onSure(Date date) {
                pay_time.setText(DateUtil.dateFormat(date.getTime()));
                latest_time.setText(DateUtil.dateFormat(date.getTime()));
            }
        });
        dialog.show();
    }
}
